package com.xuecheng.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author 星河一叶Ck
 * @date 2025/4/17
 */

@Configuration
public class GlobalCorsConfig {
    /* 允许跨域调用的过滤器 */
    @Bean
    public CorsFilter corsFilter () {
        // 创建过滤器配置
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许白名单名进行跨域调用
        corsConfiguration.addAllowedOrigin("*");
        // 放行全部原始头信息
        corsConfiguration.addAllowedHeader("*");
        // 允许跨域发送cookie
        corsConfiguration.setAllowCredentials(true);
        // 允许所有请求方法调用
        corsConfiguration.addAllowedMethod("*");
        // 获取过滤实现类
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}
